#include "IoTest.h"
#include <assert.h>
#include "TestClassB.h"


IoTest::IoTest ()
{
}


IoTest::~IoTest ()
{
}

void
IoTest::firstTest ()
{
	system ("del res\\* /Q");
	_saveSomeObjects ();
	_loadObjects ();
}

void
IoTest::secondTest ()
{
	_saveModifiedObjects ();
	_saveObjectsWithNoName ();
	_loadModifiedObjects ();
}

void
IoTest::_saveSomeObjects ()
{
	P_TestClassA p_firstA = NEWOBJ (TestClassA, "firstA");
	p_firstA->val (1);
	P_TestClassA p_secondA = NEWOBJ (TestClassA, "secondA");
	p_secondA->val (2);
	P_TestClassA p_firstASameName = NEWOBJ (TestClassA, "firstA");
	p_firstASameName->val (3);
	P_TestClassB p_firstB = NEWOBJ (TestClassB, "firstB");
	p_firstB->p_a = p_firstASameName;
	p_firstB->anArray.push_back (1);
	p_firstB->anArray.push_back (10);
	P_TestClassB p_secondB = NEWOBJ (TestClassB, "secondB");
	p_secondB->p_a = p_firstASameName;
	p_secondB->anArray.push_back (-1);
	p_secondB->anArray.push_back (-10);
	p_secondB->anArray.push_back (-100);
	P_TestClassB p_emptyB = NEWOBJ (TestClassB, "emptyB");
	OOP::ResourceSaver ().saveObject (p_firstA);
	OOP::ResourceSaver ().saveObject (p_secondA);
	OOP::ResourceSaver ().saveObject (p_firstB);
	OOP::ResourceSaver ().saveObject (p_secondB);
	OOP::ResourceSaver ().saveObject (p_emptyB);
	OOP::ResourceSaver ().saveObject (p_firstASameName);
}

void
IoTest::_loadObjects ()
{
	P_TestClassA p_firstA = OOP::ResourceLoader ().loadObject ("firstA").cvt< TestClassA > ();
	P_TestClassA p_secondA = OOP::ResourceLoader ().loadObject ("secondA").cvt< TestClassA > ();
	P_TestClassB p_firstB = OOP::ResourceLoader ().loadObject ("firstB").cvt< TestClassB > ();
	P_TestClassB p_secondB = OOP::ResourceLoader ().loadObject ("secondB").cvt< TestClassB > ();
	P_TestClassB p_emptyB = OOP::ResourceLoader ().loadObject ("emptyB").cvt< TestClassB > ();
	assert (p_firstA->val () == 1);
	assert (p_secondA->val () == 2);
	assert (p_firstB->p_a == p_secondB->p_a);
	assert (p_firstB->p_a->val () == 3);
	assert (p_firstB->anArray == std::vector< int > ({ 1, 10 }));
	assert (p_secondB->anArray == std::vector< int > ({ -1, -10, -100 }));
	assert (p_emptyB->p_a == NULL && p_emptyB->anArray.empty ());
}

void
IoTest::_saveModifiedObjects ()
{
	P_TestClassA p_firstA = OOP::ResourceLoader ().loadObject ("firstA").cvt< TestClassA > ();
	P_TestClassA p_secondA = OOP::ResourceLoader ().loadObject ("secondA").cvt< TestClassA > ();
	P_TestClassB p_firstB = OOP::ResourceLoader ().loadObject ("firstB").cvt< TestClassB > ();
	P_TestClassB p_secondB = OOP::ResourceLoader ().loadObject ("secondB").cvt< TestClassB > ();
	p_firstA->val (10);
	OOP::ResourceSaver ().saveObject (p_firstA);
	OOP::ResourceSaver ().saveObject (p_secondA);
	OOP::ResourceSaver ().saveObject (p_firstB);
	OOP::ResourceSaver ().saveObject (p_secondB);
}

void
IoTest::_saveObjectsWithNoName ()
{
	OOP::ResourceSaver ().saveObject (NEWOBJ_NONAME (TestClassA));
	OOP::ResourceSaver ().saveObject (NEWOBJ_NONAME (TestClassB));
}

void
IoTest::_loadModifiedObjects ()
{
	P_TestClassA p_firstA = OOP::ResourceLoader ().loadObject ("firstA").cvt< TestClassA > ();
	P_TestClassB p_firstB = OOP::ResourceLoader ().loadObject ("firstB").cvt< TestClassB > ();
	P_TestClassB p_secondB = OOP::ResourceLoader ().loadObject ("secondB").cvt< TestClassB > ();
	assert (p_firstA->val == 10);
	assert (p_firstB->p_a == NULL);
	assert (p_secondB->anArray == std::vector< int > ({ -1, -10, -100, -1000 }));
}
